mylist = [1, 4, -5, 10, -7, 2, 3, -1]
print([item for item in mylist if item > 0])

#列表推导耗内存，当数据量大时可使用生成器表达式迭代产生过滤的元素
filted_list = []
pos = (item for item in mylist if item > 0)
for i in pos:
    filted_list.append(i)
print(filted_list)

#当过滤条件复杂的时候，可使用filter函数
def is_int(val):
    try:
        x = int(val)
        return True
    except Exception as e:
        return False

values = ['1', '2', '-3', '-', '4', 'N/A', '5']
ivals = list(filter(is_int, values))
print(ivals)

from itertools import compress


addresses = [
'5412 N CLARK',
'5148 N CLARK',
'5800 E 58TH',
'2122 N CLARK'
'5645 N RAVENSWOOD',
'1060 W ADDISON',
'4801 N BROADWAY',
'1039 W GRANVILLE',
]

counts = [ 0, 3, 10, 4, 1, 7, 6, 1]

more5 = [i > 5 for i in counts ]
print(list(compress(addresses, more5)))
